package com.sele.seleniumGener;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

import com.sele.browers.IBrowerDriver;
import com.sele.dataprovider.ExcelDataProvider;
import com.sele.dataprovider.IDataProvider;
import com.sele.utils.CustomUtils;

public class SeleniumGeneratorImp implements ISeleniumGenerator{
	private static final Logger logger = LoggerFactory.getLogger(SeleniumGeneratorImp.class);
	private String webSite="website";
	private IBrowerDriver browerDriver;
	private WebDriver driver;
	//查找页面元素最大查找次数，超过该次数则不再寻找，防止一直等待。
	private static final int Max_TIMES=10;
	private static final String ASSERT="assert";
	
	public void doOperate(IDataProvider provider){
		if(provider instanceof ExcelDataProvider){
			doExcelOperate(provider);
		}
	}
	
	public void doExcelOperate(IDataProvider provider){
		
		logger.info("This is excel test");
		
		driver.manage().window().maximize();
		driver.get(webSite);
		List<String> info_list = null;
		String[] columnName = provider.getColumnName();
		String locateMode="";
		String entity="";
		String operateMethod="";
		String testData = "";
		String action = "";
		while(provider.hasNext() == true){
			info_list =  provider.next();
			
			int info_size = info_list.size();
			
			for(int i=0; i<info_size; i++){
				//
				if(columnName[i].contains("定位方式")){
					locateMode = info_list.get(i);
				}
				else if(columnName[i].contains("测试对象实体")){
					entity = info_list.get(i);
				}
				else if(columnName[i].contains("操作方法")){
					operateMethod = info_list.get(i);
				}
				else if(columnName[i].contains("测试数据")){
					testData = info_list.get(i);
				}
				//判断是否是断言
				else if(columnName[i].contains("校验方法")){
					if(StringUtils.isNotBlank(info_list.get(i))){
						action =ASSERT;
					}
				}
				else if(columnName[i].contains("测试描述")){
					if("断言".equals(info_list.get(i))){
						action =ASSERT;
					}
				}
			}
			//针对每一行的数据，执行相应的的操作
			if("".equals(action)){
				doNotAssert(locateMode, entity, operateMethod, testData);
			}else if(ASSERT.equals(action)){
				doAssert(locateMode, entity, operateMethod, testData);
			}
			
		}
		CustomUtils.close(driver);
	}
	
	/**
	 * 如果是断言，进行判断
	 */
	public void doAssert(String locateMode,String entity,String operateMethod,String testData){
		//获取元素
		WebElement element = getElementWithinTimes(locateMode, entity, Max_TIMES);
		if("notnull".equalsIgnoreCase(operateMethod)){
			Assert.assertNotNull(element);
		}
		//此处判断还有些问题，element是各对象
		/*else if("assertequals".equalsIgnoreCase(operateMethod)){
			Assert.assertEquals(element,testData);
		}*/
		
		
		
		
	}
	/**
	 * 如果是非断言，执行非断言方法,即执行一般的查找、点击、输入
	 */
	public void doNotAssert(String locateMode,String entity,String operateMethod,String testData){
		//遍历完一遍后获取元素和方法
		WebElement element = getElementWithinTimes(locateMode, entity, Max_TIMES);
		doElementOperation(element, operateMethod, testData);
		logger.info("执行了"+operateMethod+"方法,传入数据为:"+testData);
	}
	
	/**
	 * 在规定次数内查找，超出次数则抛出异常
	 * @param locateMode
	 * @param entity
	 * @param times
	 * @return
	 */
	public WebElement getElementWithinTimes(String locateMode,String entity,int times){
		WebElement element;
		int count = 0;
		while(true){
			element = getElement(locateMode, entity);
			if(element!=null){
				logger.info("通过"+locateMode+"的方式找到element:"+entity);
				//将寻找次数清零
				count=0;
				break;
			}
			else{
				count++;
				logger.info("第"+count+"次,"+"未通过"+locateMode+"的方式找到element"+entity+",sleep1000秒");
				
				//小于最大次数，则等待
				if(count<times){
					CustomUtils.sleep(1000);
				}else{
					throw new UnsupportedOperationException("cann't find element "+entity+" by "+locateMode+"."); 
				}
			}
		}
		return element;
	}
	
	/**
	 * 
	 * @param locateMode 定位方式
	 * @param entity 定位方式需要传入的数据
	 * @return
	 */
	public WebElement getElement(String locateMode,String entity){
		WebElement element = null;
		if("id".equalsIgnoreCase(locateMode)){
			element = driver.findElement(By.id(entity));
			
		}
		else if("linktext".equalsIgnoreCase(locateMode)){
			element = driver.findElement(By.linkText(entity));
			
		}
		else if("name".equalsIgnoreCase(locateMode)){
			element = driver.findElement(By.name(entity));
			
		}
		else if("cssselector".equalsIgnoreCase(locateMode)){
			element = driver.findElement(By.cssSelector(entity));
			
		}
		else if("xpath".equalsIgnoreCase(locateMode)){
			element = driver.findElement(By.xpath(entity));
		}
		
		return element;
		
		
	}
	public void doElementOperation(WebElement element,String operateMethod,String testData){
		
		if("sendkeys".equalsIgnoreCase(operateMethod)){
			element.sendKeys(testData);
		}
		else if("click".equalsIgnoreCase(operateMethod)){
			element.click();
		}
		else if("submit".equalsIgnoreCase(operateMethod)){
			element.submit();
		}
		
		
	}
	
	
	
	

	//不采用构造方法传参,在context中设置driver

	public IBrowerDriver getBrowerDriver() {
		return browerDriver;
	}

	public void setBrowerDriver(IBrowerDriver browerDriver) {
		this.browerDriver = browerDriver;
		//设置驱动
		setDriver(browerDriver.getDriver());
	}

	public WebDriver getDriver() {
		return driver;
	}

	public void setDriver(WebDriver driver) {
		this.driver = driver;
	}
	
	

}
